package com.unitd.base.frame.plugin.db.datasource.context;

import com.unitd.base.frame.plugin.db.datasource.bean.DataSourceInfo;
import com.unitd.base.frame.plugin.db.datasource.factory.DruidPropertiesFactory;
import com.unitd.base.frame.plugin.db.datasource.properties.DruidProperties;
import com.unitd.base.frame.spring.SpringContextHolder;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.sql.DataSource;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.unitd.base.frame.plugin.db.datasource.context.DataSourceContext.MASTER_DATASOURCE_NAME;

/**
 * mybatis的sqlSessionFactory的上下文容器(单例)
 *
 * @author Hudan
 * @version 1.0
 * @filename SqlSessionFactoryContext.java
 * @copyright www.unitd.com
 * @date 2020-06-28
 */
public class SqlSessionFactoryContext {

    /**
     * 数据库操作句柄工厂
     */
    private static Map<Object, SqlSessionFactory> sqlSessionFactories = new ConcurrentHashMap<>();

    /**
     * 添加sqlSessionFactory
     *
     * @param name              数据库操作句柄名
     * @param sqlSessionFactory 数据库操作句柄工厂
     */
    public static void addSqlSessionFactory(String name, SqlSessionFactory sqlSessionFactory) {
        sqlSessionFactories.put(name, sqlSessionFactory);
    }

    /**
     * 添加sqlSessionFactory
     *
     * @param name         数据库操作句柄名
     * @param databaseInfo 动态数据源信息
     */
    public static void addSqlSessionFactory(String name, DataSourceInfo databaseInfo) {

        // 创建properties
        DruidProperties druidProperties = DruidPropertiesFactory.createDruidProperties(databaseInfo);

        // 创建dataSource
        DataSource dataSource = DataSourceContext.createDataSource(name, druidProperties);
        DataSourceContext.addDataSource(name, dataSource);

        // 创建sqlSessionFactory
        SqlSessionFactoryCreator sqlSessionFactoryCreator = SpringContextHolder.getBean(SqlSessionFactoryCreator.class);
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryCreator.createSqlSessionFactory(dataSource);
        SqlSessionFactoryContext.addSqlSessionFactory(name, sqlSessionFactory);

        sqlSessionFactories.put(name, sqlSessionFactory);
    }

    /**
     * 获取所有的sqlSessionFactory
     *
     * @return sqlSessionFactory
     */
    public static Map<Object, SqlSessionFactory> getSqlSessionFactorys() {
        return sqlSessionFactories;
    }

    /**
     * 初始化数据库中的数据源的SqlSessionFactory
     *
     * @param sqlSessionFactoryCreator 数据库操作句柄
     */
    public static void initBaseSqlSessionFactory(SqlSessionFactoryCreator sqlSessionFactoryCreator) {

        // 获取数据库的数据源
        Map<String, DataSource> dataSources = DataSourceContext.getDataSources();

        // 创建数据库中数据源的sqlSessionFactory
        for (Map.Entry<String, DataSource> entry : dataSources.entrySet()) {
            String dbName = entry.getKey();
            DataSource dataSource = entry.getValue();

            // 如果是主数据源，跳过，否则会冲突
            if (!MASTER_DATASOURCE_NAME.equals(dbName)) {
                SqlSessionFactory sqlSessionFactory = sqlSessionFactoryCreator.createSqlSessionFactory(dataSource);
                SqlSessionFactoryContext.addSqlSessionFactory(dbName, sqlSessionFactory);
            }
        }
    }
}